package cn.edu.nankai.bookstore.dao.impi;

import cn.edu.nankai.bookstore.dao.CustomerDao;
import cn.edu.nankai.bookstore.dao.OrderDao;
import cn.edu.nankai.bookstore.domain.po.Customer;
import cn.edu.nankai.bookstore.domain.po.Order;

import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderDaoImpl extends GenericBaseDao implements OrderDao {
    @Override
    public Order findById(Integer id) {
        Order order = null;
        try {
            this.getConnection();
            String sql = "Select * from `order` where order_id = ?";
            executeQuery(sql, id);
            if (rs != null && rs.next()) {
                order = new Order(rs.getInt(1),
                        rs.getInt("customer_id"),
                        rs.getTimestamp("deal_time"),
                        rs.getInt("receiver_id"),
                        rs.getDouble("total_price"),
                        rs.getDouble("discount_total_price"),
                        rs.getBoolean("submit")
                );
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return order;
    }

    @Override
    public List<Order> findAll() {
        List<Order> orders = null;
        try {
            this.getConnection();
            //ItemDAO itemDAO=new ItemDAOImpl();
            String sql = "Select * from `order`";
            this.executeQuery(sql);
            if (rs != null) {
                orders = new ArrayList<>();
                while (rs.next()) {
                    Order order = new Order(rs.getInt(1),
                            rs.getInt("customer_id"),
                            rs.getTimestamp("deal_time"),
                            rs.getInt("receiver_id"),
                            rs.getDouble("total_price"),
                            rs.getDouble("discount_total_price"),
                            rs.getBoolean("submit")
                    );
                    orders.add(order);
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }

    @Override
    public List<Order> findBySql(String sql, Object... params) {
        List<Order> orders = null;
        try {
            this.getConnection();
            this.executeQuery(sql, params);
            if (rs != null) {
                orders = new ArrayList<>();
                while (rs.next()) {
                    orders.add(new Order(rs.getInt(1),
                            rs.getInt("customer_id"),
                            rs.getTimestamp("deal_time"),
                            rs.getInt("receiver_id"),
                            rs.getDouble("total_price"),
                            rs.getDouble("discount_total_price"),
                            rs.getBoolean("submit")
                    ));
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }

    @Override
    public int insert(Order order) {
        int res = 0;
        try {
            this.getConnection();
            String sql = "insert into `order` values(null,?,now(),?,?,?,?)";
            executeInsert(sql, order.getCustomerId(),
                    order.getReceiverId(), order.getTotalPrice(),
                    order.getDiscountTotalPrice(), order.isSubmitted()
            );
            res = this.result;
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }

    @Override
    public int update(Order order) {
        int res = 0;
        try {
            this.getConnection();
            String sql = "update `order` set customer_id = ? ," +
                    "deal_time= ? , receiver_id = ? ," +
                    "total_price = ? , discount_total_price = ? ," +
                    "submit = ? where order_id=?";
            executeUpdate(sql, order.getCustomerId(), order.getDealTime(),
                    order.getReceiverId(), order.getTotalPrice(),
                    order.getDiscountTotalPrice(), order.isSubmitted(),order.getOrderId()
            );
            res = this.result;
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Integer id) {
        int res = 0;
        try {
            this.getConnection();
            String sql = "delete from `order` where order_id = ?";
            executeUpdate(sql, id);
            res = this.result;
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return res;
    }

    @Override
    public int delete(Order order) {
        if (order != null)
            return delete(order.getOrderId());
        else
            return 0;
    }

    @Override
    public List<Order> findAllByTel(BigDecimal telephone) {
        CustomerDao customerDao = new CustomerDaoImpl();
        Customer customer = customerDao.findByTel(telephone);
        if (customer == null)
            return null;
        else {
            return findAllByCustomerId(customer.getCustomerId());
        }
    }

    @Override
    public List<Order> findAllByCustomerId(Integer customerId) {
        List<Order> orders = null;
        try {
            this.getConnection();
            String sql = "Select * from `order` where customer_id = ?";
            this.executeQuery(sql, customerId);
            if (rs != null) {
                orders = new ArrayList<>();
                while (rs.next()) {
                    Order order = new Order(rs.getInt(1),
                            rs.getInt("customer_id"),
                            rs.getTimestamp("deal_time"),
                            rs.getInt("receiver_id"),
                            rs.getDouble("total_price"),
                            rs.getDouble("discount_total_price"),
                            rs.getBoolean("submit")
                    );
                    orders.add(order);
                }
            }
            this.closeAll();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return orders;
    }
}
